package com.dg11185.nearshop.activity;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources.NotFoundException;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewStub;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.nearshop.R;

/**
 * Activity模板
 * 
 * @author wanliang527
 * @date 2014-4-28
 * @deprecated 不用了
 */
public class TemplateActivity extends Activity {
    protected final static int NO_PIC = -1;
    public final static int LEFT = 1;
    public final static int MIDDLE = 2;
    public final static int RIGHT = 3;

    private LayoutInflater mInflater;
    private LinearLayout mContainer, leftContainer;
    private TextView leftView, middleView, rightView;

    private Activity mActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = this;
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(R.layout.activity_template);
        leftView = (TextView) findViewById(R.id.txt_v_title_left);
        middleView = (TextView) findViewById(R.id.txt_v_title_middle);
        rightView = (TextView) findViewById(R.id.txt_v_title_right);
        mContainer = (LinearLayout) findViewById(R.id.llayout_container);

        leftContainer = (LinearLayout) findViewById(R.id.llayout_left);

        mContainer.addView(view);
    }

    @Override
    public void setContentView(View view, LayoutParams params) {
        view.setLayoutParams(params);
        this.setContentView(view);
    }

    @Override
    public void setContentView(int layoutResID) {
        View view = mInflater.inflate(layoutResID, null);
        this.setContentView(view);
    }

    public void setLeftCusView(View view) {
        if (leftContainer != null) {
            leftContainer.addView(view);
        }
    }

    public void setIcon(int pos, int icon) {
        switch (pos) {
            case LEFT:
                setBg(leftView, icon);
                break;

            case MIDDLE:
                setBg(middleView, icon);
                break;

            case RIGHT:
                setBg(rightView, icon);
                break;

            default:
                break;
        }
    }

    private void setBg(View view, int icon) {
        view.setClickable(true);
        view.setBackgroundResource(icon);
    }

    public void setLeft(String title, int icon) {
        setTextView(leftView, title, icon);
    }

    public void setLeft(String title) {
        setLeft(title, NO_PIC);
    }

    public void setMiddle(String title, int icon) {
        setTextView(middleView, title, icon);
    }

    public void setMiddle(String title) {
        setMiddle(title, NO_PIC);
    }

    public void setRight(String title, int icon) {
        setTextView(rightView, title, icon);
    }

    public void setRight(String title) {
        setRight(title, NO_PIC);
    }

    private void setTextView(TextView textView, String title, int icon) {
        String str = title == null ? "" : title;
        textView.setText(str);
        textView.setBackgroundResource(R.drawable.selector_at_btn_bg);
        if (icon == -1)
            return;
        try {
            Drawable drawable = getResources().getDrawable(icon);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(),
                    drawable.getMinimumHeight());
            textView.setCompoundDrawables(drawable, null, null, null);
            textView.setClickable(true);
        } catch (NotFoundException e) {
            return;
        } finally {
            textView.setBackgroundResource(R.drawable.selector_at_btn_bg);
        }
    }

    public void setClickListener(int pos, View.OnClickListener listener) {
        switch (pos) {
            case LEFT:
                leftView.setOnClickListener(listener);
                break;
            case MIDDLE:
                middleView.setOnClickListener(listener);
                break;
            case RIGHT:
                rightView.setOnClickListener(listener);
                break;
            default:
                break;
        }
    }

    public View getView(int position) {
        switch (position) {
            case LEFT:
                return leftView;
            case MIDDLE:
                return middleView;
            case RIGHT:
                return rightView;
            default:
                return null;
        }
    }
}
